import { getMetaTaskData, getMetaPlanData } from '@/api/assessment/index'
import Flow from '@/views/components/flow.vue'
import CustomTable from '@/components/CustomTable/index.vue'
import { GetPlanTasksList } from '@/api/decisionManage'

export default {
  components: {
    Flow,
    CustomTable
  },
  data() {
    return {
      visible: false,
      tableData: [],
      znjcJsonStr: {
        // 用于回显计算出的权重
        rwwcl: '',
        gcsxxjz: '',
        gcsxxfc: '',
        wxfzjh: '',
        cbjjz: '',
        cbjfc: '',
        tygdjjz: '',
        tygdjfc: '',
        final: 0
      },
      decisionColumns: [
        {
          label: '任务类型',
          prop: 'rwlx'
        },
        {
          label: '目标形态',
          prop: 'mbxt'
        },
        {
          label: '气象条件',
          prop: 'qxtj'
        },
        {
          label: '攻击因素',
          prop: 'gjys'
        },
        {
          label: '是否合作目标',
          prop: 'sfhzmb'
        },
        {
          label: '载荷类型',
          prop: 'zhlx'
        },
        {
          label: '工作模式',
          prop: 'gzms'
        },
        {
          label: '分辨率(m)',
          prop: 'fbl'
        },
        {
          label: '优先级',
          prop: 'yxj'
        }
      ],
      decisionData: [],
      decisionChildColumns: [
        {
          prop: 'expand'
        },
        {
          label: '目标编号',
          prop: 'mbbh'
        },
        {
          label: '经度',
          prop: 'pointJd'
        },
        {
          label: '纬度',
          prop: 'pointWd'
        }
      ],
      recordInputWeights: {
        // 用于回显所选择的权重
        rwwcl: '',
        gcsxxjz: '',
        gcsxxfc: '',
        wxfzjh: '',
        cbjjz: '',
        cbjfc: '',
        tygdjjz: '',
        tygdjfc: '',
      }
    }
  },
  computed: {},
  created() { },
  mounted() {
    this.getMetaTaskData()
    this.GetPlanTasksList()
    this.Auto()
  },
  methods: {
    stringToBool(str) {
      return str === 'true'
    },
    // 自动化
    Auto() {
      const flag = this.$route.query.flag
      if (this.stringToBool(flag) == false) {
        return
      }
      const params = {
        rwwcl: 0.125,
        gcsxxjz: 0.125,
        gcsxxfc: 0.125,
        wxfzjh: 0.125,
        cbjjz: 0.125,
        cbjfc: 0.125,
        tygdjjz: 0.125,
        tygdjfc: 0.125,
        final: 0
      }

      this.getEstimateResult(params)
      // 用于回显八个结果当时输入的权重
      this.recordInputWeights = params
    },
    // 获取需求决策数据
    GetPlanTasksList() {
      if (!localStorage.getItem('xqid')) {
        return
      }
      GetPlanTasksList({
        xqid: localStorage.getItem('xqid')
      }).then(response => {
        if (response.data.code !== 200) {
          this.$message.error(response.data.message)
          return
        }
        this.decisionData = response.data.data.records
      })
    },
    // 点击评估计算
    countResult() {
      let count = 0
      for (const key in this.recordInputWeights) {
        if (this.recordInputWeights[key] == '') {
          count++
        }
      }
      if (count == 8) {
        return this.$message.warning("请配置权重再计算")
      }
      // 把手动配置好的权重传递过去
      this.getEstimateResult(this.recordInputWeights)
    },
    // 指标树配置确定后调用
    getEstimateResult(stringData) {
      // 大于90条，直接截取90条，不大于全传
      const tableSlice =
        Array.isArray(this.tableData) && this.tableData.length > 80
          ? this.tableData.slice(0, 80)
          : this.tableData
      const params = {
        zbsJsonStr: JSON.stringify(stringData).replace(/\"/g, "'"),
        znjcJsonStr: JSON.stringify(tableSlice).replace(/\"/g, "'")
      }
      getMetaPlanData(params).then(response => {
        if (response.data.code !== 200) {
          this.$message.error(response.data.message)
          return
        }
        if (response.data.data == '') {
          return this.$message.warning("请配置权重再计算")
        }

        const data = JSON.parse(response.data.data)
        this.znjcJsonStr = data // 回显计算结果权重
        this.$message.success('操作成功')
      })
    },
    // 指标树配置回调
    weightCallback(list) {
      let arr = list
      const obj = JSON.parse(JSON.stringify(this.znjcJsonStr))

      arr.forEach((item, index) => {
        let objKey = Object.keys(obj)[index] // 获取对象的键名（基于索引）
        let objValue = item.value // 获取对应的值
        obj[objKey] = Number(objValue) // 将对象的值添加到数组元素中
      })

      // this.getEstimateResult(obj)
      // 用于回显八个结果当时输入的权重
      this.recordInputWeights = obj
    },

    /** 打开指标树配置 */
    handleIndicatorWeight() {
      this.$refs.assessmentflow.dialogVisible = true
    },

    handleOpenBottom() {
      this.visible = !this.visible
    },

    // 获取元任务列表
    getMetaTaskData() {
      if (
        !this.$route.query.tjTime ||
        localStorage.getItem('yrwbh') === undefined
      ) {
        return this.$message.warning('请在任务规划完成元任务选择后再进入')
      }
      getMetaTaskData({
        tjTime: this.$route.query.tjTime,
        yrwbh: localStorage.getItem('yrwbh')
      }).then(response => {
        if (response.data.code !== 200) {
          this.$message.error(response.data.message)
          return
        }
        this.tableData = response.data.data
      })
    }
  }
}
